<!DOCTYPE html>
<html lang="en">
<head>
<title>用户管理</title>
	<div th:include="base-libs-easyui :: base-libs-easyui" />
	<script th:src="'/console/js/md5.js'"></script>
<style type="text/css">
body {
	padding: 0px 10px;
}

.datagrid-header-row, .datagrid-row {
	height: 32px;
}

.datagrid-row-selected{
	background-color: #3071A9;
}

.tree-icon{
	display: none;
}
</style>

<script th:inline="javascript">
	/**读取request中的初始化数据*/
	var requestData = [[${requestData}]];
	var roleAry = requestData["roleAry"];
</script>
<script type="text/javascript">
	$(function() {
		initView();
		initData();
	});
	
	function initView() {
	    $("#formUser").attr("action", contextRoot + "/user/save");
		$("#btnSave").on("click", btnSaveHandler);
		$("#btnBack").on("click", btnBackHandler);
		$("#ROLEID").on("change", roleIdChangeHandler);

		/* easyui树  */
        $('#treeMenu').treegrid({
            idField : 'MENUID',
            treeField : 'MENUNAME',
            animate : true,
            fitColumns : true,
            border : false,
            scrollbarSize : 2,
            columns : [ [
                {
                    title : '菜单编码',
                    field : 'MENUID',
                    width : '80'
                }
                ,{
                    title : '菜单名称',
                    field : 'MENUNAME',
                    width : '220'
                },{
                    title : '菜单功能',
                    field : 'ACTIONSARY',
                    width : '400'
                    ,formatter: function(value,row,index){
                        var actionCheckBoxs = [];
                        var MENUID = row["MENUID"];
						if(value)
						{
							$.each(value, function(){
								var text = this["ACTION_NAME"];
								var checkbox = "<label style='padding-right: 8px; margin-bottom: 0px; display: block;'>"
									+"<input id='" + this["CONTROL_ID"] + "' class='actionCheckbox' menuid='" + MENUID + "' type='checkbox' style='vertical-align: middle; margin-top: auto; width: 14px; background-color: transparent; border: none;'>" +
									"<span style='vertical-align: middle;'>" + text + "</span></label>";
								actionCheckBoxs.push($(checkbox).prop("outerHTML"));
							})
						}
                        return actionCheckBoxs.join("");
                    }
                }
			]]
        });
	}

	/**改变角色动态查询角色对应的菜单*/
	function roleIdChangeHandler(){
	    var roleId = $(this).val();
	    queryRoleMenu(roleId);
	}
	function queryRoleMenu(roleId){
	    $.QuickRemote.AjaxJson(contextRoot + "/user/queryRoleMenu", queryRoleMenuHandler, {ROLEID: roleId});
	}
	function queryRoleMenuHandler(result){
	    if(result && result.length>0){
            var dsTree = createTreeList(result, result[0]["PRE_MENUID"]);
            $('#treeMenu').treegrid('loadData', dsTree).treegrid('expandAll');
		}
	}

	function btnSaveHandler()
	{
		if($(".form-control").QuickValidate())
		{
		    var params = {};

		    var password = $("#PASSWORD").val();
		    if(password != '↭➽⇝↫↠⟰'){ //特殊符号
				$("#HEX_PASSWORD").val(hex_md5($("#PASSWORD").val()));
			}
			/*原始信息*/
			var yljgry = $("#formUser").data("yljgry");
			var username = yljgry["USERNAME"];
			if(StringUtils.isEmpty(username)){
                params["method"] = "insert";
			}else{
                params["method"] = "update";
			}
			$.QuickRemote.AjaxFiledFormSubmit("formUser", saveHandler, params);
		}
	}
	
	function saveHandler(result, addParams)
	{
		var state = result["state"];
		if(state)
		{
			$.QuickAlert.alertNormal("保存成功", {callback: function(){
                $("#btnBack").trigger("click");
			}});
		}
		else
		{
			var info = result["info"];
			$.QuickAlert.alertFail({content: info["faultInfo"]});
		}
	}

	/**返回回调*/
	function btnBackHandler()
	{
		//$.QuickUrlUtils.redirectUrl("index.html");
        //location = contextRoot + "/user/index.html";
        $.QuickUrlUtils.back();
	}

	function initData() {
		$("#ROLEID").createOptions(roleAry, {needNull: "true", textField: "ROLENAME", valueField: "ROLEID"});
        var yljgry = StoreCache.removeCache("cache_yljgry");
        $("#formUser").data("yljgry", yljgry);
		$(".form-control").each(function(){
			var name = $(this).attr("name");
			var value = yljgry[name];
			$(this).QuickField("setData", value);
		});
		var username = yljgry["USERNAME"];
		if(!StringUtils.isEmpty(username)){
            $("#PASSWORD").val("↭➽⇝↫↠⟰");  //用特殊符号占位
            $("#ROLEID").trigger("change");
		}
	}

	/**封装树形结构*/
	function createTreeList(list, sjjgdm) {
		var childList = [];
		for (var i = 0; i < list.length; i++) {
			var item = list[i];
			if (item == null) {
				continue;
			}
			var sjjgdm1 = item["PRE_MENUID"];
			var ACTIONS = item["ACTIONS"];
			if(!StringUtils.isEmpty(ACTIONS))
			{
                ACTIONS = JSON.parse(ACTIONS);
                item["ACTIONSARY"] = ACTIONS;
                delete item["ACTIONS"];
			}
			if (sjjgdm1 != sjjgdm) {
				continue;
			} else {
				var resid = item["MENUID"];
				item["text"] = item["MENUNAME"];
				item["id"] = item["MENUID"];
				var lt = createTreeList(list, resid);
				if (lt != null && lt.length != 0) {
					item["children"] = lt;
				}
				childList.push(item);
				list[i] = null;
			}
		}
		if (childList.length == 0) {
			childList = null;
		}
		return childList;
	}
</script>
</head>
<body class="easyui-layout">
	<div region="north" style="height: 41px; border: none; overflow: hidden; background-color: #f9f9f9;">
		<div class="title">
			<h3>分配用户信息</h3>
		</div>
	</div>
	<div region="center" style="border: none;">
		<div class="easyui-layout" fit="true">
			<div region="north" style="height: 240px; overflow: hidden;" data-options="collapsible:false">
				<div style="padding: 6px 0px;">
					<button id="btnBack" style=" margin-left: 10px;" type="button">返回</button>
					<button id="btnSave" style=" margin-left: 10px;" type="button">保存</button>
				</div>
				<div class="easyui-panel" style="height: 120px;" title="人员基本信息">
					<table class="form-table">
						<tr>
							<td class="td-text">
								<label>机构名称</label>
							</td>
							<td class="td-content">
								<input class="form-control" name="YLJGMC" readonly="readonly"/>
							</td>
							<td class="td-text">
								<label>科室名称</label>
							</td>
							<td class="td-content">
								<input class="form-control" name="KSMC" readonly="readonly"/>
							</td>
							<td class="td-text">
								<label>工号</label>
							</td>
							<td class="td-content">
								<input class="form-control" name="YSGH" readonly="readonly"/>
							</td>
						</tr>
						<tr>
							<td class="td-text">
								<label>姓名</label>
							</td>
							<td class="td-content">
								<input class="form-control" name="XM" readonly="readonly"/>
							</td>
							<td class="td-text">
								<label>性别</label>
							</td>
							<td class="td-content">
								<input class="form-control" name="XBMC" readonly="readonly"/>
							</td>
							<td class="td-text">
								<label>身份证号</label>
							</td>
							<td class="td-content">
								<input class="form-control" name="SFZH" readonly="readonly"/>
							</td>
						</tr>
					</table>
				</div>
				<div class="easyui-panel" style="height: 80px; border-top: none;" title="用户信息">
					<form id="formUser">
						<table class="form-table">
							<tr>
								<td class="td-text">
									<label>登录名</label>
								</td>
								<td class="td-content">
									<!--<input  style="display: none;">-->
									<input id="USERNAME" name="USERNAME" autocomplete="new-username"
										   quick-config="label:'用户名', validateProxy:'notNull length', length: '100'"
										   class="form-control"/>
									<input id="USERID" class="form-control" name="USERID" style="display: none;"/>
								</td>
								<td class="td-text">
									<label>密码</label>
								</td>
								<td class="td-content">
									<input id="PASSWORD" type="password" autocomplete="new-password"
										   quick-config="label:'密码', validateProxy:'notNull length', length: '20'"
										   class="form-control">
									<input id="HEX_PASSWORD" class="form-control" name="PASSWORD" style="display: none;" />
								</td>
								<td class="td-text">
									<label>角色</label>
								</td>
								<td class="td-content">
									<select id="ROLEID" name="ROLEID"
											quick-config="label:'角色', validateProxy:'notNull'"
											class="form-control"></select>
								</td>
							</tr>
						</table>
					</form>
				</div>
			</div>
			<div region="center" title="角色菜单信息">
				<table id="treeMenu" fit="true"></table>
			</div>
		</div>
	</div>
</div>
</body>
</html>

